<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" @submit.prevent>
      <el-form-item label="站点名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入站点名称"
          clearable
          @keyup.enter="handleQuery"
          v-hasPermi="['jzzgl:zdgl:list']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['jzzgl:zdgl:list']">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['jzzgl:zdgl:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="stationList">
      <el-table-column label="站点名称" align="center" prop="stationName" />
      <el-table-column label="站点地址" align="center" prop="stationAddress" />
      <el-table-column label="站点配置密码" align="center" prop="stationPassword" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jzzgl:zdgl:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jzzgl:zdgl:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stationFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="站点名称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="站点地址" prop="stationAddress">
          <el-input v-model="form.stationAddress" placeholder="请输入站点地址" />
        </el-form-item>
        <el-form-item label="站点配置密码" prop="stationPassword">
          <el-input v-model="form.stationPassword" placeholder="请输入站点配置密码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input
            v-model="form.contactPhone"
            placeholder="请输入联系电话"
            maxlength="20"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listStation, delStation, addStation, updateStation } from "@/api/jzzgl/station";
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)
const total = ref(0)
const stationList = ref([])
const showSearch = ref(true)

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  stationName: undefined
})

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    stationName: undefined
  };
  getList();
}

const getList = async () => {
  loading.value = true;
  try {
    const res = await listStation(queryParams.value);
    stationList.value = res.rows;
    total.value = res.total;
  } catch (error) {
    console.error("获取站点列表失败", error);
  } finally {
    loading.value = false;
  }
}

const handleDelete = (row) => {
  const stationId = row.id;
  ElMessageBox.confirm('是否确认删除该站点？', "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(function() {
    return delStation(stationId);
  }).then(() => {
    getList();
    ElMessage.success("删除成功");
  }).catch(() => {});
}

const open = ref(false);
const title = ref("");
const stationFormRef = ref();
const form = ref({
  id: undefined,
  stationName: undefined,
  stationAddress: undefined,
  stationPassword: undefined,
  contactPerson: undefined,
  contactPhone: undefined
});

const rules = ref({
  stationName: [
    { required: true, message: "站点名称不能为空", trigger: "blur" },
    { min: 2, max: 50, message: "站点名称长度必须在 2 到 50 个字符之间", trigger: "blur" }
  ],
  stationAddress: [
    { required: false, message: "请输入站点地址", trigger: "blur" }
  ],
  stationPassword: [
    { required: true, message: "请输入站点配置密码", trigger: "blur" },
    { min: 6, max: 20, message: "站点配置密码长度必须在 6 到 20 个字符之间", trigger: "blur" }
  ],
  contactPerson: [
    { required: false, message: "请输入联系人", trigger: "blur" }
  ],
  contactPhone: [
    {required: false,message: "请输入联系电话",trigger: "blur"}
  ]
});

const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加站点";
};

const handleUpdate = (row) => {
  reset();
  const stationId = row.id;
  form.value = { ...row };
  open.value = true;
  title.value = "修改站点";
};

const reset = () => {
  form.value = {
    id: undefined,
    stationName: undefined,
    stationAddress: undefined,
    stationPassword: undefined,
    contactPerson: undefined,
    contactPhone: undefined
  };
  if (stationFormRef.value) {
    stationFormRef.value.resetFields();
  }
};

const cancel = () => {
  open.value = false;
  reset();
};

const submitForm = () => {
  stationFormRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        if (form.value.id) {
          await updateStation(form.value);
          ElMessage.success("修改成功");
        } else {
          await addStation(form.value);
          ElMessage.success("新增成功");
        }
        open.value = false;
        getList();
      } catch (error) {
        console.error(error);
        ElMessage.error("操作失败");
      }
    }
  });
};

onMounted(() => {
  getList();
})
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
.dialog-footer {
  padding: 20px;
  text-align: right;
}
</style>
